<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot="searchMenu" slot-scope="{row,size}">
        <el-button :size="size" type="primary" icon="el-icon-connection" @click="handleAisle(row)">更换通道</el-button>
      </template>
      <template slot-scope="{ row }" slot="isOn">
        <el-switch
          :active-value="1"
          :inactive-value="0"
          v-model="row.isOn"
          @change="handleSwitch(row)"
        >
        </el-switch>
      </template>
    </avue-crud>
    <el-dialog
      title="数据导入"
      append-to-body
      :visible.sync="excelBox"
      width="555px"
    >
      <avue-form
        :option="excelOption"
        v-model="excelForm"
        :upload-after="uploadAfter"
      >
        <template slot="excelTemplate">
          <el-button type="primary" @click="handleTemplate">
            点击下载<i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog>
  </basic-container>
</template>

<script>
// import {
//   queryCompDetailByRpi,
//   getDetail,
//   addRpi,
//   updateRpi,
//   remove,
//   changeStaffStatus,
// } from "@/api/rb-merchant/posStaff/pos_staff";
import {
  queryCompDetailByRpi,
  onOrOffPayCompanySkill,
  changeDefaultSkillAisle
} from "@/api/rb-merchant/posinfo/pos_info_details";
// import { getList as getMerchantList } from "@/api/rb-merchant/posinfo/pos_info";
import { getList as getStoreList } from "@/api/rb-merchant/posStore/pos_store";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";

export default {
  data() {
    return {
      form: {},
      query: {},
      search: {
        aisleCode: '',
      },
      loading: true,
      excelBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      aisleRpiNo: null,
      option: {
        // height: "auto",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        menu: false,
        addBtn: false,
        viewBtn: false,
        delBtn: false,
        emptyBtn: false,
        // selection: true,
        columnBtn: false,
        labelWidth: 120, //弹出表单的标题的统一宽度
        searchBtn: false,
        // searchBtnText: '更换通道',
        // searchBtnIcon: 'el-icon-connection',
        searchLabelWidth: 100, //搜索框标题宽度
        searchMenuSpan: 6,
        searchMenuPosition: 'left',
        menuWidth: "300",
        column: [
          {
            label: "主键",
            prop: "id",
            type: "input",
            //sortable: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            hide: true,
            overHidden: true,
          },
          {
            label: "支付类型",
            prop: "skillName",
            type: "input",
            //sortable: true,
            display: false,
            overHidden: true,
          },
          {
            label: "支付通道",
            prop: "aisleName",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入支付通道",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "默认使用通道",
            prop: "aisleCode",
            type: "select",
            // dicUrl: "/api/blade-system/dict/dictionary?code=is_on",
            props: {
              label: "aisleName",
              value: "aisleCode",
            },
            display: false,
            search: true,
            hide: true,
            clearable: true,
            dicData: [],
            rules: [
              {
                required: true,
                message: "请选择通道编号",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "通道费率",
            prop: "rate",
            slot: true,
            display: false,
            overHidden: true,
          },
          {
            label: "操作",
            prop: "isOn",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=is_on",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            display: false,
            rules: [
              {
                required: true,
                message: "请输入是否启用",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          // {
          //   label: "",
          //   prop: "createTime",
          //   type: "input",
          //   //sortable: true,
          //   addDisplay: false,
          //   editDisplay: false,
          //   viewDisplay: false,
          //   hide: true,
          // },
        ],
      },
      data: [],
      excelForm: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "模板上传",
            prop: "excelFile",
            type: "upload",
            drag: true,
            loadText: "模板上传中，请稍等",
            span: 24,
            propsHttp: {
              res: "data",
            },
            tip: "请上传 .xls,.xlsx 标准格式文件",
            action: "/api/rb-merchant/merchant/posStaff/pos_staff/import",
          },
          {
            label: "模板下载",
            prop: "excelTemplate",
            formslot: true,
            span: 24,
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.pos_staff_add, false),
        viewBtn: this.vaildData(this.permission.pos_staff_view, false),
        delBtn: this.vaildData(this.permission.pos_staff_delete, false),
        editBtn: this.vaildData(this.permission.pos_staff_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  created() {
  },
  methods: {
    /* 提交手动修改密码 */
    getPassUpdate() {
      
    },
    /* 修改密码 */
    // handlePassword(row) {
    // },
    /* 关闭 */
    handlePassClose(done) {
      done();
    },
    /* 获取门店列表信息 */
    getStoreInfo() {
      getStoreList("-1", "-1", { rpiNo: this.$route.query.rpiNo }).then(
        (res) => {
          const column = this.findObject(this.option.column, "rpsNo");
          column.dicData = res.data.data.records;
        }
      );
    },
    /**
     * @func 支付公司配置状态开关
     * @desc 支付公司配置状态开关 0否 1是
     * @param {Object} row - 支付公司信息
     * */
    handleSwitch(row) {
      // rpiNo, aisleCode, compNo, skillType, isOn
      let params = {
        rpiNo: this.aisleRpiNo,
        aisleCode: row.aisleCode,
        compNo: row.compNo,
        skillType: row.skillType,
        isOn: row.isOn
      }
      onOrOffPayCompanySkill(params).then(
        () => {},
        (error) => {
          row.isOn = row.isOn == 1 ? 0 : 1;
          window.console.log(error);
        }
      );
    },
    /**
     * @func 更换默认使用通道
     * @desc 更换默认使用通道 选中后点击按钮触发
     * */
    handleAisle(row) {
      // return console.log(row);
      if (row.aisleCode) {
        let params = {
          aisleCode: row.aisleCode,
          rpiNo: this.aisleRpiNo,
          // compNo: row.compNo,
        }
        changeDefaultSkillAisle(params).then((res) => {
          this.onLoad(this.page, this.search);
          this.$message({
            type: "success",
            message: res.data.msg
          });
        });
      } else {
        this.$message.warning("请选择通道后再更换");
      }
    },
    rowSave(row, done, loading) {
      row.rpiNo = this.$route.query.rpiNo;
      if (this.cashierRpsNo) {
        row.rpsNo = this.cashierRpsNo;
      }
      // addRpi(row).then(
      //   () => {
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: "success",
      //       message: "操作成功!",
      //     });
      //     done();
      //   },
      //   (error) => {
      //     loading();
      //     window.console.log(error);
      //   }
      // );
      loading();
    },
    rowUpdate(row, index, done, loading) {
      // updateRpi(row).then(
      //   () => {
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: "success",
      //       message: "操作成功!",
      //     });
      //     done();
      //   },
      //   (error) => {
      //     loading();
      //     window.console.log(error);
      //   }
      // );
      loading();
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        // getDetail(this.form.id).then((res) => {
        //   this.form = res.data.data;
        // });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      let options = JSON.parse(decodeURIComponent(this.$route.query.params));
      this.aisleRpiNo = options.rpiNo;
      queryCompDetailByRpi(
        Object.assign(params, options, this.query)
      ).then((res) => {
        const data = res.data.data;
        // this.page.total = data.total;
        const column = this.findObject(this.option.column, "aisleCode");
        column.dicData = data.aisleVOS;
        this.data = data.list;
        this.search.aisleCode = data.aisleCode;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleImport() {
      this.excelBox = true;
    },
    handleExport() {
      let exQuery = ``;
      exQuery += `&rpstNo=${this.search.rpstNo}`;
      exQuery += `&rpstName=${this.search.rpstName}`;
      exQuery += `&rpiNo=${this.search.rpiNo}`;
      exQuery += `&rpsNo=${this.search.rpsNo}`;
      exQuery += `&userNo=${this.search.userNo}`;
      exQuery += `&isOn=${this.search.isOn}`;
      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        window.open(
          `/api/rb-merchant/merchant/posStaff/pos_staff/export?${
            this.website.tokenHeader
          }=${getToken()}` + exQuery
        );
      });
    },
    handleTemplate() {
      window.open(
        `/api/rb-merchant/merchant/posStaff/pos_staff/export-template?${
          this.website.tokenHeader
        }=${getToken()}`
      );
    },
    uploadAfter(res, done) {
      this.excelBox = false;
      this.refreshChange();
      done();
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
  },
};
</script>

<style>
</style>
